package com.yxy.ms.manager.web;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yxy.ms.manager.common.Common;
import com.yxy.ms.manager.common.Results;
import com.yxy.ms.manager.entity.po.BillRecord;
import com.yxy.ms.manager.entity.po.BillTemp;
import com.yxy.ms.manager.service.BillService;

@RestController
@RequestMapping(value = "/bill/")
public class BillController {

    @Autowired
    BillService billService;

    /**
     * <函数描述>ErrorData异常信息数据
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getBillList", method = RequestMethod.POST)
    public Results getBillList(@RequestBody String data) {
        Page<BillTemp> list = billService.findAll(conditional(data));
        return Common.results200(getPageData(list));
    }

    /**
     * <函数描述>ErrorData异常信息数据
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getBillRecordList", method = RequestMethod.POST)
    public Results getBillRecordList(@RequestBody String data) {
        Page<BillRecord> list = billService.findBillRecordList(conditional(data));
        return Common.results200(getPageData(list));
    }

    protected JSONObject conditional(String data) {
        JSONObject root = JSON.parseObject(data);
        JSONObject page = root.getJSONObject("page");
        JSONObject params = root.getJSONObject("params");

        if (page != null) {
            PageHelper.startPage(null != page.getInteger("pageIndex") ? page.getInteger("pageIndex") : 1,
                null != page.getInteger("pageSize") ? page.getInteger("pageSize") : 10);
            PageHelper.orderBy(StringUtils.isEmpty(page.getString("orderBy")) ? "" : page.getString("orderBy"));
        }

        return params;
    }

    @SuppressWarnings("unchecked")
    protected <T> T conditional(String data, Class<?> classType) {
        JSONObject root = JSON.parseObject(data);
        JSONObject params = root.getJSONObject("params");
        return (params != null ? (T)params.toJavaObject(classType) : null);
    }

    protected Map<String, Object> getPageData(Page<?> page) {
        Map<String, Object> result = new HashMap<String, Object>();
        // 总记录数
        result.put("total", page.getTotal());
        // 总页数
        result.put("pages", page.getPages());
        // 每页记录数
        result.put("pageSize", page.getPageSize());
        // 当前页码
        result.put("pageNum", page.getPageNum());
        // 结果
        result.put("rows", page.getResult());
        return result;
    }

}
